package com.yuntian.controller;

import com.yuntian.entity.StaffMaster;
import com.yuntian.service.StaffMasterService;
import com.yuntian.entity.DepartmentMaster;
import com.yuntian.service.DepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.servlet.http.HttpSession;

/**
 * 员工控制器
 * @author yuntian
 */
@Controller
@RequestMapping
@RequiredArgsConstructor
public class StaffController {

    private final StaffMasterService staffMasterService;
    private final DepartmentService departmentService;

    /**
     * 登录
     * @param staffMaster
     * @return
     */
    @PostMapping("login")
    public String login(StaffMaster staffMaster, HttpSession session){
        boolean login = staffMasterService.login(staffMaster);
        if(login){
            // 登录成功后，设置会话中的登录信息（用于菜单权限控制）
            String deptCode = staffMaster.getM00304();
            DepartmentMaster dept = null;
            try {
                dept = departmentService.getDepartmentByCode(deptCode);
            } catch (Exception ignored) {}
            session.setAttribute("loginUserCode", staffMaster.getM00301());
            session.setAttribute("loginDeptCode", deptCode);
            session.setAttribute("loginDeptName", dept != null ? dept.getM00502() : null); // 部门名称
            return "index";
        }
        return "redirect:/department/form";
    }
}
